/**   
 * @Title: ImageUtils.java 
 * @Package com.esun.util.img 
 * @Description: 用一句话描述该文件做什么 
 * @author Miko baoz@500wan.com   
 * @date 2011-3-23 下午06:07:45  
 */
package com.esun.util.img;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.esun.config.Config;
import com.esun.util.img.db.ImageDB;
import com.esun.util.log.EDebug;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

/**
 * @ClassName: ImageUtils
 * @Description: 这里用一句话描述这个类的作用
 * @author Miko baoz@500wan.com
 * @date 2011-3-23 下午06:07:45
 * 
 */
public class ImageUtils {

	// 图片下载地址
	private static final String PIC_ADDRESS = Config.PIC_ADDRESS;
	
	// private static final String PIC_ADDRESS =
	// "http://192.168.65.202/rabbit2/resource/identifier/";

	// private static final String PIC_ADDRESS =
	// "http://192.168.65.202/rabbit2/resource/identifier/";

	// 图片存储位置
	private static final String PIC_PATH = Config.PIC_PATH;

	// 最大图片存储数量
	private static final int PIC_SAVE_MAX = Config.PIC_SAVE_MAX;

	// 图片下载超时时间
	private static final int SERVER_TIME_OUT = Config.SERVER_TIME_OUT;

	// 保存图片
	public static boolean saveImg(String name, Bitmap img) {

		if (ImageDB.getImageDB().getDBLength() > PIC_SAVE_MAX) {

			String ifFirst = ImageDB.getImageDB().getFirstLine();
			ImageDB.getImageDB().deleteImg(ifFirst);
		}

		ImageDB.getImageDB().insertImg(name, img);

		return false;

	}

	// 获得图片
	public static Bitmap getLocalImg(String name) {

		if (ImageDB.getImageDB().isExist(name)) {
			byte[] imgByte = ImageDB.getImageDB().getData(name);

			Bitmap bmp = null;

			try {
				EDebug.o("|--------- do get image ....(local) ---------|");
				bmp = BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
			} catch (OutOfMemoryError oom) {

				EDebug.o("|--------- Try decreacing first ....(local) ---------|");
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inSampleSize = 2;
				try {
					bmp = BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length, options);
				} catch (OutOfMemoryError oomSec) {
					EDebug.o("|--------- Try decreacing second ....(local) ---------|");
					options.inSampleSize = 4;
					try {
						bmp = BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length, options);
					} catch (OutOfMemoryError oomNull) {

						EDebug.o("|--------- Try decreacing img give up ....(local) ---------|");

					}
				}
			}

			return bmp;
		} else {

			return null;
		}

	}

	// 下载图片
	public static Bitmap downLoadImg(String imgName) {

		EDebug.o(ImageUtils.class, "Starting downloading image.");

		Bitmap bitmap = null;

		InputStream is = null;

		HttpURLConnection conn = null;

		try {

			conn = (HttpURLConnection) new URL(PIC_ADDRESS + imgName
			// +".png"
			).openConnection();
			conn.setConnectTimeout(SERVER_TIME_OUT);
			conn.setReadTimeout(SERVER_TIME_OUT);

			conn.connect();

			is = conn.getInputStream();

			EDebug.o(ImageUtils.class, " ************ getting inputstream available is : " + is.available());

			try {
				EDebug.o("|--------- do get image ....(NET) ---------|");
				bitmap = BitmapFactory.decodeStream(is);
			} catch (OutOfMemoryError oom) {
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inSampleSize = 2;
				try {
					EDebug.o("|--------- Try decreacing first ....(NET) ---------|");
					bitmap = BitmapFactory.decodeStream(is, null, options);
				} catch (OutOfMemoryError mmoSec) {
					options.inSampleSize = 4;
					try {
						EDebug.o("|--------- Try decreacing second ....(NET) ---------|");
						bitmap = BitmapFactory.decodeStream(is, null, options);
					} catch (OutOfMemoryError mmoNull) {

						EDebug.o("|--------- Try decreacing img give up ....(NET) ---------|");

					}

				}

			}

			EDebug.o(ImageUtils.class, "Starting download image. success");

			return bitmap;

		} catch (Exception e) {

			EDebug.o(ImageUtils.class, "Starting download image. failed" + e.getLocalizedMessage());

			return null;
		}
	}
	
	// 下载图片
	public static Bitmap downLoadImg(String imgName , String address) {

		EDebug.o(ImageUtils.class, "Starting downloading image.");

		Bitmap bitmap = null;

		InputStream is = null;

		HttpURLConnection conn = null;

		try {

			conn = (HttpURLConnection) new URL(address + imgName
			// +".png"
			).openConnection();
			conn.setConnectTimeout(SERVER_TIME_OUT);
			conn.setReadTimeout(SERVER_TIME_OUT);

			conn.connect();

			is = conn.getInputStream();

			EDebug.o(ImageUtils.class, " ************ getting inputstream available is : " + is.available());
			try {
				EDebug.o("|--------- do get image ....(NET) ---------|");
				bitmap = BitmapFactory.decodeStream(is);
			} catch (OutOfMemoryError oom) {
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inSampleSize = 2;
				try {
					EDebug.o("|--------- Try decreacing first ....(NET) ---------|");
					bitmap = BitmapFactory.decodeStream(is, null, options);
				} catch (OutOfMemoryError mmoSec) {
					options.inSampleSize = 4;
					try {
						EDebug.o("|--------- Try decreacing second ....(NET) ---------|");
						bitmap = BitmapFactory.decodeStream(is, null, options);
					} catch (OutOfMemoryError mmoNull) {

						EDebug.o("|--------- Try decreacing img give up ....(NET) ---------|");

					}

				}

			}

			EDebug.o(ImageUtils.class, "Starting download image. success");

			return bitmap;

		} catch (Exception e) {

			EDebug.o(ImageUtils.class, "Starting download image. failed" + e.getLocalizedMessage());

			return null;
		}
	}

	// 删除图片
	public void deleteImg() {

		String firstId = ImageDB.getImageDB().getFirstLine();

		ImageDB.getImageDB().deleteImg(firstId);

	}

	// 获取本地图片总数
	public int getLocalImgCount() {
		return ImageDB.getImageDB().getDBLength();
	}

	public boolean saveIcon(String name, Bitmap img) {

		BufferedOutputStream bos = null;
		try {

			bos = new BufferedOutputStream(new FileOutputStream(PIC_PATH + name));

			img.compress(Bitmap.CompressFormat.PNG, 100, bos);
			bos.flush();
			bos.close();

			return true;

		} catch (FileNotFoundException e1) {
			return false;

		} catch (IOException e) {
			return false;
		}

	}

	public static Drawable getLocalIMG(String imgName, Context context) {

		try {
			context.openFileInput(imgName);
			return Drawable.createFromPath(PIC_PATH + imgName);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			return null;
		}
	}

	public static boolean deleteIMG(String imgName) {

		File file = new File(PIC_PATH + imgName);
		if (file.isFile() && file.exists()) {
			file.delete();
			EDebug.o(ImageUtils.class, "delete image seccess -> [" + imgName + "]");
			return true;
		} else {
			EDebug.o(ImageUtils.class, "delete image failed -> [" + imgName + "]");
			return false;
		}
	}

}
